<template>
  <div>
    <avue-data-box class="wrap" :option="boxoption"></avue-data-box>
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :option="tableOption"
        :simple-page="true"
        @on-load="getList"
        @size-change="sizeChange"
        @current-change="currentChange"
        :table-loading="listLoading"
        @search-change="handleFilter"
        @refresh-change="handleRefreshChange"
        @search-reset="handleRefreshChange"
      >
        <template slot="guanli" slot-scope="scope">
          {{manageFn(scope.row)}}
        </el-switch>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { BigNumber } from 'bignumber.js'
import { fetchList, rate } from "@/api/admin/caiwu";
import { tableOption } from "@/const/crud/admin/caiwu";
import { mapGetters } from "vuex";

export default {
  name: "token",
  data() {
    return {
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      listLoading: false,
      tableOption: tableOption,
      rate: 0.6,
      param: {},
      boxoption: {
        span: 8,
        data: [
          {
            title: "账户总收入",
            count: 0,
            decimals: 2,
            icon: "el-icon-takeaway-box",
            color: "rgb(56, 161, 242)"
          },
          {
            title: "待交易金额",
            count: 0,
            decimals: 2,
            icon: "el-icon-c-scale-to-original",
            color: "rgb(56, 161, 242)"
          },
          {
            title: "回购金额",
            count: 0,
            decimals: 2,
            icon: "el-icon-picture-outline-round",
            color: "rgb(56, 161, 242)"
          },
          {
            title: "已交易金额",
            count: 0,
            decimals: 2,
            icon: "el-icon-picture-outline-round",
            color: "rgb(56, 161, 242)"
          },
          {
            title: "总成交金额",
            count: 0,
            decimals: 2,
            icon: "el-icon-goods",
            color: "rgb(56, 161, 242)"
          },
          {
            title: "本月收入",
            count: 0,
            decimals: 2,
            icon: "el-icon-date",
            color: "rgb(56, 161, 242)"
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        delBtn: this.vaildData(this.permissions.sys_token_del, false)
      };
    }
  },
  methods: {
    getList(page, params) {
      this.listLoading = true;
      fetchList(
        Object.assign(
          {
            page: page.currentPage,
            size: page.pageSize
          },
          this.param
        )
      ).then(response => {
        this.tableData = response.data.data.data;
        this.page.total = response.data.data.count || '0';
        this.listLoading = false;

        this.boxoption.data[0].count = response.data.data.totalIncome/100
        this.boxoption.data[1].count = response.data.data.sellTotal
        this.boxoption.data[2].count = response.data.data.huigouTotal
        this.boxoption.data[3].count = response.data.data.jiaoyiTotal
        this.boxoption.data[4].count = response.data.data.dealTotal
        this.boxoption.data[5].count = response.data.data.totalMonth/100
        
      });
      rate().then(res=>{
        this.rate = res.data.data.percent1
      })
    },
    rowDel: function(row, index) {
      this.$confirm("是否强制" + row.username + "下线?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delObj(row.access_token);
        })
        .then(data => {
          this.$message.success("删除成功");
        });
    },
    manageFn(item){
      return BigNumber(item.price).multipliedBy(BigNumber(this.rate)).multipliedBy(BigNumber(item.days)).dividedBy(360).toNumber().toFixed(2)
    },
     handleFilter(param, done) {
      this.page.currentPage = 1;
      this.param = param
      if(this.param.create_time){
        this.param.start = this.param.create_time[0]
        this.param.end = this.param.create_time[1]
        delete this.param.create_time
      }
      this.getList(this.page, param);
      done();
    },
    handleRefreshChange() {
      this.getList(this.page);
    },
    refreshChange() {
      this.getList(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    }
  }
};
</script>
<style lang="less">
.wrap {
  padding: 20px;
}
</style>
